package com.yyw.Service;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.yyw.bean.User;
import com.yyw.dao.ClassDao;
import com.yyw.dao.UserDao;
import com.yyw.util.MyContext;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private ClassDao classDao;

    @Value("${my.session.user}")
    private String userAttr;


    @Transactional
    public Result updateUserClassInfo(User user) {

        int i = userDao.updateUserClassInfo(user);
        if (i > 0) {
            return ResultFactory.success("修改成功");
        } else {
            return ResultFactory.failure("修改失败");
        }
    }

    @Transactional
    public Result updateUser(User user) {
        // 数据校验
        if (StringUtils.isEmpty(user.getName())) {
            user.setName(null);
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            user.setPassword(null);
        }
        if (StringUtils.isEmpty(user.getSex())) {
            user.setSex(null);
        }
        if (StringUtils.isEmpty(user.getEmail())) {
            user.setEmail(null);
        }
        if (user.getSex() != null && !(user.getSex().equals("男") || user.getSex().equals("女"))) {
            return ResultFactory.notice("性别只能是男或女");
        }

        int i = userDao.updateUser(user);
        if (i > 0) {
            return ResultFactory.success("修改成功");
        } else {
            return ResultFactory.failure("修改失败");
        }

    }

    public Result login(String tel, String password) {
        // 数据校验
        if (StringUtils.isEmpty(password)) {
            return ResultFactory.notice("密码为空");
        }
        if (StringUtils.isEmpty(tel)) {
            return ResultFactory.notice("手机号为空");
        }

        User user = userDao.getUserByTel(tel);
        if (user == null) {
            return ResultFactory.notice("用户不存在");
        }

        if (user.getPassword().equals(password)) {
            user.setPassword(null);
            HttpSession session = MyContext.getHttpSession();
            session.setAttribute(userAttr, user);
            return ResultFactory.success("登录成功", user);
        } else {
            return ResultFactory.notice("密码错误");
        }
    }

    /**
     * 注册用户,注册时需要传入name,password,"type",tel
     *
     * @param user
     * @return
     */
    @Transactional
    public Result register(User user) {
        // 数据校验
        if (StringUtils.isEmpty(user.getName())) {
            return ResultFactory.notice("用户名为空");
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            return ResultFactory.notice("密码为空");
        }
        if (!user.checkType()) {
            return ResultFactory.notice("用户类型不合法");
        }
        if (StringUtils.isEmpty(user.getTel())) {
            return ResultFactory.notice("手机号为空");
        }

        // 根据手机号查询用户是否已经注册
        User user2 = userDao.getUserByTel(user.getTel());
        if (user2 != null) {
            return ResultFactory.notice("改手机号已被注册");
        }

        // 注册
        int result = userDao.saveUser(user);

        if (result > 0) {
            return ResultFactory.success("注册成功");
        } else {
            return ResultFactory.failure("注册失败");
        }

    }
}
